<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('订单列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="case-form">
                <div class="select-list">
                    <ul>
                        <li>
                            <p style="width: 90px;">订单号：</p>
                            <input type="text" name="orderSn"/>
                        </li>
                        <li>
                            <p style="width: 90px;">会员编号：</p>
                            <input type="text" name="memberSn"/>
                        </li>
                        <li shiro:hasPermission="back:shop:list">
                            <p style="width: 90px;">站点名称：</p>
                            <input type="text" name="shopName"/>
                        </li>
                        <li shiro:hasPermission="back:shop:list">
                            <p style="width: 90px;">店铺昵称：</p>
                            <input type="text" name="shopCode"/>
                        </li>
                        <li shiro:hasPermission="back:member:list">
                            <p style="width: 90px;">微信昵称：</p>
                            <input type="text" name="nickname"/>
                        </li>
                        <li shiro:hasPermission="back:member:list">
                            <p style="width: 90px;">会员名：</p>
                            <input type="text" name="userName"/>
                        </li>
                        <li>
                            <p style="width: 90px;">手机号：</p>
                            <input type="text" name="receiverPhone"/>
                        </li>
                        <li>
                            <p style="width: 90px;">订单状态：</p>
                            <select name="orderStatus" th:with="type=${@dict.getType('order_status')}">
                                <option value="-1">全部</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <p style="width: 110px;">用户支付状态：</p>
                            <select name="payStatus" th:with="type=${@dict.getType('pay_status')}">
                                <option value="-1">全部</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <p style="width: 110px;">商家支付状态：</p>
                            <select name="shopPayStatus" th:with="type=${@dict.getType('pay_status')}">
                                <option value="-1">全部</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li class="select-time">
                            <p style="width: 90px">下单时间： </p>
                            <input type="text" class="time-input" placeholder="开始时间"
                                   name="params[beginTime]"/>
                            <span>-</span>
                            <input type="text" class="time-input" placeholder="结束时间"
                                   name="params[endTime]"/>
                        </li>

                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success single disabled " onclick="changeExtraRemark()"
               shiro:hasPermission="back:order:extraRemark">
                <i class="fa fa-edit"></i> 编辑平台备注
            </a>
            <a class="btn btn-success multiple disabled" onclick="delivery()" shiro:hasPermission="back:order:send">
                <i class="fa fa-send"></i> 物流信息
            </a>
            <a class="btn btn-primary multiple disabled" onclick="complete()" shiro:hasPermission="back:order:edit">
                <i class="fa fa-check"></i> 批量完成
            </a>
            <a class="btn btn-info " onclick="$.table.importExcel()" shiro:hasPermission="back:order:import">
                <i class="fa fa-upload"></i> 导入发货
            </a>
            <a class="btn btn-warning multiple disabled" onclick="exportSelect()"
               shiro:hasPermission="back:order:export">
                <i class="fa fa-download"></i> 选择导出
            </a>
            <a class="btn btn-warning multiple disabled" onclick="printNoteSelect()"
               shiro:hasPermission="back:order:printNote">
                <i class="fa fa-download"></i> 选择打印小票
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<!-- 导入区域 -->
<script id="importTpl" type="text/template">
    <form enctype="multipart/form-data" class="mt20 mb10">
        <div class="col-xs-offset-1">
            <input type="file" id="file" name="file"/>
            <div class="mt10 pt5">
                <input type="checkbox" id="updateSupport" name="updateSupport" title="如果快递信息已填写，将会覆盖更新。"> 是否覆盖已填写的快递信息
                &nbsp; <a onclick="$.table.importTemplate()" class="btn btn-default btn-xs"><i
                    class="fa fa-file-excel-o"></i> 下载模板</a>
            </div>
            <font color="red" class="pull-left mt10">
                提示：仅允许导入“xls”或“xlsx”格式文件！
            </font>
        </div>
    </form>
</script>
<script th:inline="javascript">
    const prefix = ctx + "back/order";
    const sendFlag = [[${@permission.hasPermi('back:order:send')}]];
    const closeFlag = [[${@permission.hasPermi('back:order:close')}]];
    const editFlag = [[${@permission.hasPermi('back:order:edit')}]];
    const orderStatusDict = [[${@dict.getType('order_status')}]];
    const stockStatusDict = [[${@dict.getType('stock_status')}]];
    const payStatusDict = [[${@dict.getType('pay_status')}]];

    $(function () {
        var options = {
            url: prefix + "/searchList",
            createUrl: prefix + "/add",
            detailUrl: prefix + "/detail/{id}",
            exportUrl: prefix + "/export",
            importUrl: prefix + "/importLogistics",
            importTemplateUrl: prefix + "/importLogisticsTemplate",
            modalName: "订单",
            sortName: "createTime",
            sortOrder: "desc",
            height: 600,
            showColumns: true,
            paginationShowPageGo: true,
            detailView: true,
            clickToSelect: true,
            onExpandRow: function (index, row, $detail) {
                initChildTable(index, row, $detail);
            },
            columns: [
                {
                    checkbox: true
                },
                {
                    field: 'idStr',
                    title: '订单ID',
                    visible: false
                },
                {
                    field: 'orderSn',
                    title: '订单号',
                    formatter: function (value, row, index) {
                        return '<a style="text-decoration:underline" href="javascript:void(0)" onclick="$.operate.detail(\'' + value + '\',1000)">' + value + '</a>';
                    }
                },
                {
                    field: 'shopCode',
                    title: '店铺昵称'
                },
                {
                    field: 'shopName',
                    title: '店铺名称',
                },
                {
                    field: 'memberSn',
                    title: '用户信息',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<span style="font-weight: bold">' + row.memberSn + '</span><br/>');
                        let imgHtml = $.common.sprintf("<img class='img-circle img-xs' data-height='%s' data-width='%s' data-target='%s'  style='border-radius: 0;' src='%s'/>", 500, 'auto', 'self', row.avatar);
                        actions.push(imgHtml);
                        actions.push('<br/><span style="font-weight: bold">' + row.nickname + '</span>');
                        return actions.join('');
                    }
                },
                {
                    field: 'userName',
                    title: '会员名',
                },
                {
                    field: 'orderStatus',
                    title: '订单状态',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(orderStatusDict, value);
                    }
                },
                {
                    field: 'buyCost',
                    title: '订单金额',
                    width: '18%',
                    sortable: true,
                    formatter: function (value, row, index) {
                        var actions = [];
                        var deliveryCost = 0.0;
                        if(null != row.deliveryCost){
                            deliveryCost = row.deliveryCost;
                        }
                        actions.push('<label>进货总价：</label><span style="font-weight: bold">' + value + '</span>');
                        actions.push('<br/><label>销售总价：</label><span style="font-weight: bold">' + row.productCost + '</span>');
                        actions.push('<br/><label>物流费：</label><span style="font-weight: bold">' + deliveryCost + '</span>');
                        actions.push('<br/><label>实付金额：</label><span style="font-weight: bold">' + row.payCost + '</span>');
                        actions.push('<br/><label>店铺毛利：</label><span style="font-weight: bold">' + Math.round(row.payCost-deliveryCost-value) + '</span>');
                        return actions.join('');
                    }
                },
                {
                    field: 'detailAddress',
                    title: '收货信息',
                    width: '18%',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<label>收货人：</label><span style="font-weight: bold">' + row.receiver + '</span>');
                        actions.push('<br/><label>收货电话：</label><span style="font-weight: bold">' + row.receiverPhone + '</span>');
                        actions.push('<br/><label>详细地址：</label><span style="font-weight: bold">' + row.receiveDistricts + row.receiveAddress + '</span>');
                        return actions.join('');
                    }
                },
                {
                    field: 'payStatus',
                    title: '用户付款信息',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<label>付款状态：</label><span style="font-weight: bold">' + $.table.selectDictLabel(payStatusDict, value) + '</span>');
                        if (parseInt(value) === 1) {
                            actions.push('<br/><label>付款时间：</label><span style="font-weight: bold">' + row.payTime + '</span>');
                        }
                        return actions.join('');
                    }
                },
                {
                    field: 'shopPayStatus',
                    title: '店铺付款信息',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<label>付款状态：</label><span style="font-weight: bold">' + $.table.selectDictLabel(payStatusDict, value) + '</span>');
                        if (parseInt(value) === 1) {
                            actions.push('<br/><label>付款时间：</label><span style="font-weight: bold">' + row.shopPayTime + '</span>');
                        }
                        return actions.join('');
                    }
                },
                {
                    field: 'orderRemark',
                    title: '买家备注',
                    sortable: true
                },
                {
                    field: 'shopRemark',
                    title: '商家备注',
                    visible: false
                },
                {
                    field: 'extraRemark',
                    title: '平台备注',
                    visible: false
                },
                {
                    field: 'isEnable',
                    title: '库存状态',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(stockStatusDict, value);
                    }
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    sortable: true
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        switch (parseInt(row.orderStatus)) {
                            //未支付
                            case 0:
                                if (0 === parseInt(row.isEnable)) {
                                    actions.push('<a class="btn btn-primary btn-xs ' + editFlag + '" href="#" onclick="edit(\'' + row.idStr + '\',1,0)">设为有货</i></a> ');
                                } else {
                                    actions.push('<a class="btn btn-danger btn-xs ' + editFlag + '" href="#" onclick="edit(\'' + row.idStr + '\',1,1)">设为缺货</i></a> ');
                                }
                                break;
                            //已支付
                            case 1:
                                if (1 === parseInt(row.shopPayStatus)) {
                                    actions.push('<a class="btn btn-info btn-xs ' + sendFlag + '" href="#" onclick="delivery(\'' + row.idStr + '\')"><i class="fa fa-send">物流信息</i></a> ');
                                    actions.push('<a class="btn btn-danger btn-xs ' + closeFlag + '" href="#" onclick="refund(\'' + row.orderSn + '\')"><i class="fa fa-backward">订单退款</i></a> ');
                                }
                                if (0 === parseInt(row.isEnable)) {
                                    actions.push('<a class="btn btn-primary btn-xs ' + editFlag + '" href="#" onclick="edit(\'' + row.idStr + '\',1,0)">设为有货</i></a> ');
                                } else {
                                    actions.push('<a class="btn btn-danger btn-xs ' + editFlag + '" href="#" onclick="edit(\'' + row.idStr + '\',1,1)">设为缺货</i></a> ');
                                }
                                break;
                            //已发货
                            case 2:
                                actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="deliveryEdit(\'' + row.idStr + '\',2,null)"><i class="fa fa-edit">物流信息</i></a> ');
                                actions.push('<a class="btn btn-primary btn-xs ' + editFlag + '" href="#" onclick="complete(\'' + row.orderSn + '\',2,null)"><i class="fa fa-check">已完成</i></a> ');
                            default:
                                break;
                        }
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="edit(\'' + row.orderSn + '\',2,null)"><i class="fa fa-edit">编辑</i></a> ');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });
    initChildTable = function (index, row, $detail) {
        var cur_table = $detail.html('<table style="table-layout:fixed"></table>').find('table');
        $(cur_table).bootstrapTable({
            url: prefix + "/detailList",
            method: 'post',
            sidePagination: "server",
            contentType: "application/x-www-form-urlencoded",   // 编码类型
            queryParams: {
                orderSn: row.orderSn
            },
            columns: [
                {
                    field: 'id',
                    title: 'ID',
                    visible: false
                },
                {
                    field: 'productSn',
                    title: '编号',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<label>商品编号：</label><span style="font-weight: bold">' + value + '</span>');
                        if (parseInt(row.productSkuId) !== 0 && row.productSkuCode != null) {
                            actions.push('<br/><label>sku编号：</label><span style="font-weight: bold">' + row.productSkuCode + '</span>');
                        }
                        return actions.join('');
                    }
                },
                {
                    field: 'productName',
                    title: '商品信息',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<span>' + value + '</span>');
                        if (parseInt(row.productSkuId) !== 0 && row.productAttr != null) {
                            let skuName = "";
                            let skuJsonData = JSON.parse(row.productAttr);
                            for (let i = 0; i < skuJsonData.length; i++) {
                                skuName += skuJsonData[i].key + ":" + skuJsonData[i].value + ";"
                            }
                            actions.push('<br/><span style="font-weight: bold">' + skuName + '</span>');
                        }
                        return actions.join('');
                    }
                },
                {
                    field: 'productCover',
                    title: '封面图',
                    formatter: function (value, row, index) {
                        // var thumbUrl = value + '?imageView2/1/w/64/h/64';
                        return $.common.sprintf("<img class='img-circle img-sm' data-height='%s' data-width='%s' data-target='%s'  style='border-radius: 0;' src='%s' data-src='%s'/>", 400, 'auto', 'self', value, value);
                    }
                },
                {
                    field: 'productBuyPrice',
                    title: '进货金额',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<label>进货价：</label><span style="font-weight: bold">' + value + '</span>');
                        actions.push('<br/><label>数量：</label><span style="font-weight: bold">' + row.productQuantity + '</span>');
                        actions.push('<br/><label>进货总价：</label><span style="font-weight: bold">' + row.totalBuyPrice + '</span>');
                        return actions.join('');
                    }
                },
                {
                    field: 'productSkuPrice',
                    title: '销售金额',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<label>销售价：</label><span style="font-weight: bold">' + value + '</span>');
                        actions.push('<br/><label>数量：</label><span style="font-weight: bold">' + row.productQuantity + '</span>');
                        actions.push('<br/><label>销售总价：</label><span style="font-weight: bold">' + row.totalPrice + '</span>');
                        return actions.join('');
                    }
                },
                {
                    field: 'remark',
                    title: '商品备注'
                }]
        });
    };

    /* 订单发货 */
    function delivery(orderId) {
        if (orderId == undefined) {
            var rows = $.table.selectColumns('idStr');
            orderId = rows.join();
        }
        var url = prefix + '/delivery/' + orderId;
        $.modal.open("订单发货", url, 1000);
    }

    /* 订单修改 */
    function deliveryEdit(orderId) {
        var url = prefix + '/deliveryEdit/' + orderId;
        $.modal.open("订单发货修改", url, 1000);
    }


    /* 订单退款 */
    function refund(orderSn) {
        $.modal.confirm("确认退还订单号" + orderSn + "的下货金额给店铺吗？", function () {
            $.post(prefix + "/refund", {orderSn: orderSn}, function (result) {
                if (result.code == web_status.SUCCESS) {
                    $.modal.alertSuccess(result.msg)
                    $.table.refresh();
                } else if (result.code == web_status.WARNING) {
                    $.modal.alertWarning(result.msg)
                } else {
                    $.modal.alertError(result.msg);
                }
                $.modal.closeLoading();
            });
        });
    }

    /* 订单已完成 */
    function complete(orderSn) {
        var rows = [];
        if (orderSn == undefined) {
            rows = $.table.selectColumns('orderSn');
            rows.join();
        } else {
            rows[0] = orderSn;
        }
        $.modal.confirm("确认要将" + rows.length + "条订单状态改为【已完成】吗？", function () {
            var data = {};
            data.orderSnStr = rows.join()
            $.post(prefix + "/complete", data, function (result) {
                if (result.code == web_status.SUCCESS) {
                    $.modal.alertSuccess(result.msg)
                    $.table.refresh();
                } else if (result.code == web_status.WARNING) {
                    $.modal.alertWarning(result.msg)
                } else {
                    $.modal.alertError(result.msg);
                }
                $.modal.closeLoading();
            });
        });
    }

    /* 修改平台备注 */
    function changeExtraRemark() {
        var orderSnArr = $.table.selectColumns('orderSn');
        var url = prefix + '/extraRemark/' + orderSnArr[0];
        $.modal.open("修改平台备注", url);
    }

    /* 修改订单 */
    function edit(key, type, value) {
        switch (type) {
            case 1:
                isEnable = Math.abs(parseInt(value) - 1);
                $.operate.post(prefix + "/edit", {id: key, isEnable: isEnable}, function () {
                    $.table.refresh();
                });
                break;
            case 2:
                var url = prefix + "/edit/" + key;
                $.modal.open("修改订单", url, 1000);
                break;

        }
    }

    //选择数据导出
    function exportSelect() {
        var rows = $.table.selectColumns('orderSn');
        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一条记录");
            return;
        }
        $.modal.confirm("确认要导出的" + rows.length + "条数据吗？", function () {
            var data = {};
            if (rows.length === 1) {
                data.orderSn = rows[0];
            } else {
                data.orderSnStr = rows.join()
            }
            $.modal.loading("正在导出数据，请稍后...");
            $.post(prefix + "/export", data, function (result) {
                if (result.code == web_status.SUCCESS) {
                    window.location.href = ctx + "common/download?fileName=" + encodeURI(result.msg) + "&delete=" + true;
                } else if (result.code == web_status.WARNING) {
                    $.modal.alertWarning(result.msg)
                } else {
                    $.modal.alertError(result.msg);
                }
                $.modal.closeLoading();
            });
        });
    }

    //选择打印小票
    function printNoteSelect() {
        var rows = $.table.selectColumns('orderSn');
        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一条记录");
            return;
        }
        var orderSnStr;
        if (rows.length === 1) {
            orderSnStr = rows[0];
        } else {
            orderSnStr = rows.join()
        }
        $.modal.open('打印设置', ctx + "back/printer/config/1/" + orderSnStr, '600', '400');
    }
</script>
</body>
</html>